﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.BLL.Material;
using System.Data.SqlClient;
using RN.ERP.BLL;


namespace RN.ERP.Web.Store
{
    public partial class StorehouseList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            BindTree("0");
            BindCPHCode("0");
            StoreBLL sb = new StoreBLL();
            this.txtcUUid.Text =sb.GetNameByUserGuid(UserInfo.CorpUserGuid);
            this.txtcCUid.Text = sb.GetNameByUserGuid(UserInfo.CorpUserGuid);
            this.txtdCDate.Text = DateTime.Now.ToString();
            this.txtdUDate.Text = DateTime.Now.ToString();
        }


        private void BindTree(string pid)  
        {
            this.TreeView1.Nodes.Clear();
            StoreBLL sb = new StoreBLL();
            List<DTO.Storehouse> ls = sb.GetList("000");
            if (ls.Count > 0)  
            {
                for (int i = 0; i < ls.Count; i++)  
                {  
                    TreeNode node = new TreeNode();
                    node.Text = ls[i].cSHName;
                    node.Value = ls[i].cSHCode; 
                    this.TreeView1.Nodes.Add(node);  
                    BindNode(node);  
                }  
            }  
        }






        private void BindNode(TreeNode nd)  
        {
            StoreBLL sb = new StoreBLL();
            List<DTO.Storehouse> lm = sb.GetList(nd.Value);
            for (int i = 0; i < lm.Count; i++)  
            {  
                TreeNode node = new TreeNode();
                node.Text = lm[i].cSHName;
                node.Value = lm[i].cSHCode;
                nd.ChildNodes.Add(node);  
  
                BindNode(node);  
            }  
        }


        private void BindCPHCode(string pid)
        {
            this.ddlCPHCode.Items.Clear();
            ListItem li1 = new ListItem("无", "000");
            this.ddlCPHCode.Items.Add(li1);
            StoreBLL sb = new StoreBLL();
            List<DTO.Storehouse> ls = sb.GetList("000");
            if (ls.Count > 0)
            {
                for (int i = 0; i < ls.Count; i++)
                {
                    ListItem li = new ListItem(ls[i].cSHName, ls[i].cSHCode);
                    this.ddlCPHCode.Items.Add(li);
                    BindChildCPHCode(ls[i].cSHCode);
                }
            }
        }



        private void BindChildCPHCode(string pid)
        {
            StoreBLL sb = new StoreBLL();
            List<DTO.Storehouse> ls = sb.GetList(pid);
            for (int i = 0; i < ls.Count; i++)
            {
                ListItem li = new ListItem("--------------" + ls[i].cSHName, ls[i].cSHCode);
                this.ddlCPHCode.Items.Add(li);
                BindChildCPHCode(ls[i].cSHCode);
            }
        }


        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            string value = this.TreeView1.SelectedValue;
            StoreBLL sb = new StoreBLL();
            DTO.Storehouse s = sb.GetStorehouse(value);
            if (s != null)
            {
                this.txtcCUid.Text = s.cCUName;
                this.txtcUUid.Text = s.CUUName;
                this.txtcMemo.Text = s.cMemo;
                this.ddlCPHCode.SelectedValue = s.cPSHCode;
                this.txtCSHDefinCode.Text = s.CSHDefinCode;
                this.txtcSHName.Text = s.cSHName;
                this.txtdCDate.Text = s.dCDate.ToString();
                this.txtdUDate.Text = s.dUDate.ToString();
                this.hfcSHCode.Value = s.cSHCode;
                this.hfCCUID.Value = s.cCUid.ToString();
                this.hfId.Value = s.Id.ToString();
                
            }
            this.btnSave.Text = "修改";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
           StoreBLL sb=new StoreBLL();
           string cSHCode = sb.GetCSHCode();
           string cPSHCode = this.ddlCPHCode.SelectedValue == "" ? "000" : this.ddlCPHCode.SelectedValue;
           string CSHDefinCode = this.txtCSHDefinCode.Text;
           string cSHName = this.txtcSHName.Text;
           string cMemo = this.txtcMemo.Text;
           int iStatus = 0;
           string cCUid = UserInfo.CorpUserGuid;
           string cUUid = UserInfo.CorpUserGuid;

           RN.ERP.DTO.Storehouse sh = new DTO.Storehouse();
           sh.cCUid = Guid.Parse(cCUid);
           sh.cMemo = cMemo;
           sh.cPSHCode = cPSHCode;
           sh.CSHDefinCode = txtCSHDefinCode.Text.ToString();
           sh.cSHName = cSHName;
           sh.cSHCode = cSHCode;
           sh.cUUid = Guid.Parse(cUUid);
           sh.dCDate = DateTime.Now;
           sh.dUDate = DateTime.Now;
           sh.iStatus = iStatus.ToString();
           if (this.hfcSHCode.Value == "0")
           {
               RN.ERP.DTO.OperationResult result = sb.AddStorehouse(sh);
               if (result.Result == true)
               {
                   ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功')</script>");
               }
               else
               {
                   ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败')</script>");
               }
           }
           else
           {
               sh.dCDate = DateTime.Parse(this.txtdCDate.Text);
               sh.cCUid = Guid.Parse(this.hfCCUID.Value);
               sh.cSHCode = this.hfcSHCode.Value;
               sh.Id = int.Parse(this.hfId.Value);
               RN.ERP.DTO.OperationResult result = sb.EditStorehouse(sh);
               if (result.Result == true)
               {
                   ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功')</script>");
               }
               else
               {
                   ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败')</script>");
               }

           }

            BindTree("0");
            BindCPHCode("0");
        }  
    }
}